package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipSpareInWarehouse;
import cc.comac.mom.bean.EquipSpareInWarehouseBean;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipSpareInWarehouseDao;
import cc.comac.mom.entity.EquipInWarehouseSpareRelationEntity;
import cc.comac.mom.entity.EquipSpareInWarehouseEntity;
import cc.comac.mom.entity.EquipSparePartMasterDataEntity;
import cc.comac.mom.entity.EquipWarehouseStockEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.DataStatus;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class EquipSpareInWarehouseDataHandler
		extends ServiceImpl<EquipSpareInWarehouseDao, EquipSpareInWarehouseEntity> {

	@Autowired
	private EquipSpareInWarehouseDao equipSpareInWarehouseDao;

	@Autowired
	private EquipInWarehouseSpareRelationDataHandler equipInWarehouseSpareRelationDataHandler;

	@Autowired
	private EquipWarehouseStockDataHandler equipWarehouseStockDataHandler;

	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;

	@Autowired
	private EquipSparePartMasterDataDataHandler equipSparePartMasterDataDataHandler;

	@Autowired
	private UniversalNumberGenerationDataHandler universalNumberGenerationDataHandler;

	// 新增入库数据
	public ResultBean spareInwarehouse(EquipSpareInWarehouseBean equipSpareInWarehouseBean) {
		ResultBean resultBean = new ResultBean(true);
		EquipSpareInWarehouseEntity innerData = equipSpareInWarehouseBean.getEquipSpareInWarehouse().getInnerData();
		List<EquipInWarehouseSpareRelationEntity> list = equipSpareInWarehouseBean.getList();
		if (StringUtils.isEmpty(innerData.getInWarehouseNumber())) {
			String numberGeneration = universalNumberGenerationDataHandler
					.numberGeneration(universalNumberGenerationDataHandler.getInWarehouseNumberGeneration());
			innerData.setInWarehouseNumber(numberGeneration);
		}
		// 新增备件入库单数据
		baseEntityDataUtil.setBaseEntityData(innerData);
		innerData.setStatus(DataStatus.UNCONFIRMED);
		int insert = equipSpareInWarehouseDao.insert(innerData);
		if (insert != 1) {
			throw new RuntimeException("新增备件入库数据失败，新增数量" + insert);
		}
		// 新增入库单和设备数据
		for (EquipInWarehouseSpareRelationEntity equipInWarehouseSpareRelationEntity : list) {
			if (equipInWarehouseSpareRelationEntity.getQuantity() == 0
					|| equipInWarehouseSpareRelationEntity.getPrice() == 0
					|| StringUtils.isEmpty(equipInWarehouseSpareRelationEntity.getSpareLocation())) {
				throw new RuntimeException("备件位置编码,数量,单价都不能为空！");
			}
			baseEntityDataUtil.setBaseEntityData(equipInWarehouseSpareRelationEntity);
			equipInWarehouseSpareRelationEntity.setInWarehouseId(innerData.getId());
		}
		// 批量新增入库备件数据
		boolean batchInsert = equipInWarehouseSpareRelationDataHandler.batchInsert(list);
		if (!batchInsert) {
			throw new RuntimeException("新增备件失败！");
		}
		return resultBean;
	}

	// 根据入库单主键更新备件数据数量和库存
	public ResultBean updateInWarehouseSpareQuantity(EquipSpareInWarehouse euipSpareInWarehouse)
			throws RuntimeException {
		ResultBean resultBean = new ResultBean(true);
		EquipSpareInWarehouseEntity innerData = euipSpareInWarehouse.getInnerData();
		if (innerData.getId() == 0) {
			throw new RuntimeException("无法获取入库单主键信息");
		}
		// 通过入库单主键查询入库单数据
		EquipSpareInWarehouseEntity selectById = equipSpareInWarehouseDao.selectById(innerData.getId());
		if (selectById.getStatus().equals(DataStatus.CONFIRMED)) {
			throw new RuntimeException("已确认状态的入库单不可重复确认！");
		}
		ResultDev selectByInWarehouseId = equipInWarehouseSpareRelationDataHandler
				.selectByInWarehouseId(innerData.getId());
		@SuppressWarnings("unchecked")
		List<EquipInWarehouseSpareRelationEntity> list = (List<EquipInWarehouseSpareRelationEntity>) selectByInWarehouseId
				.getEntityList();
		ArrayList<EquipWarehouseStockEntity> arrayList = new ArrayList<EquipWarehouseStockEntity>();
		for (EquipInWarehouseSpareRelationEntity equipInWarehouseSpareRelationEntity : list) {
			EquipWarehouseStockEntity entity = new EquipWarehouseStockEntity();
			entity.setQuantity(equipInWarehouseSpareRelationEntity.getQuantity());
			entity.setSparePartId(equipInWarehouseSpareRelationEntity.getSparePartId());
			entity.setWarehouseName(selectById.getWarehouseName());
			entity.setLocation(equipInWarehouseSpareRelationEntity.getSpareLocation());
			arrayList.add(entity);
		}
		// 更新库存数量
		equipWarehouseStockDataHandler.updateQuatityBySpareIdAndWarehouseId(arrayList);
		ArrayList<EquipSparePartMasterDataEntity> masterList = new ArrayList<EquipSparePartMasterDataEntity>();
		for (EquipInWarehouseSpareRelationEntity equipInWarehouseSpareRelationEntity : list) {
			EquipSparePartMasterDataEntity entity = new EquipSparePartMasterDataEntity();
			entity.setTotalAmount(equipInWarehouseSpareRelationEntity.getQuantity());
			entity.setId(equipInWarehouseSpareRelationEntity.getSparePartId());
			masterList.add(entity);
		}
		// 更新备件主数据数量
		equipSparePartMasterDataDataHandler.batchUpdateCountById(masterList);

		// 更新备件数据状态为已确认
		baseEntityDataUtil.updateBaseEntityData(selectById);
		selectById.setStatus(DataStatus.CONFIRMED);
		equipSpareInWarehouseDao.updateById(selectById);
		return resultBean;
	}

	// 批量根据入库单主键更新备件数据数量和库存
	public ResultBean batchUpdateInWarehouseSpareQuantity(List<EquipSpareInWarehouse> list) throws RuntimeException {
		ResultBean resultBean = new ResultBean(true);
		for (EquipSpareInWarehouse equipSpareInWarehouse : list) {
			resultBean = updateInWarehouseSpareQuantity(equipSpareInWarehouse);
		}
		return resultBean;
	}

	// 更新入库单数据
	public ResultBean updateInWarehouseSpareData(EquipSpareInWarehouseBean euipSpareInWarehouseBean) {
		ResultBean resultBean = new ResultBean(true);
		EquipSpareInWarehouse equipSpareInWarehouse = euipSpareInWarehouseBean.getEquipSpareInWarehouse();
		EquipSpareInWarehouseEntity innerData = equipSpareInWarehouse.getInnerData();
		List<EquipInWarehouseSpareRelationEntity> list = euipSpareInWarehouseBean.getList();
		if (innerData != null) {
			if (innerData.getId() == 0) {
				throw new RuntimeException("无法获取入库单主键");
			}
			baseEntityDataUtil.updateBaseEntityData(innerData);
			innerData.setStatus(DataStatus.UNCONFIRMED);
			int updateById = equipSpareInWarehouseDao.updateById(innerData);
			if (updateById != 1) {
				throw new RuntimeException("更新数据失败，更新数量为" + updateById);
			}
		}
		if (list.size() > 0) {
			equipInWarehouseSpareRelationDataHandler.updateBatchById(list, list.size());
			Iterator it=list.iterator();
			while(it.hasNext()){
				EquipInWarehouseSpareRelationEntity e= (EquipInWarehouseSpareRelationEntity) it.next();
				if(e.getId()!=0){
					it.remove();
				}else{
					baseEntityDataUtil.setBaseEntityData(e);
				}
			}
			if(!list.isEmpty()){
				boolean batchInsert = equipInWarehouseSpareRelationDataHandler.batchInsert(list);
				if (!batchInsert) {
					throw new RuntimeException("新增备件失败！");
				}
			}
		}
		return resultBean;
	}

	// 批量逻辑删除数据
	public ResultBean batchDelete(List<EquipSpareInWarehouse> inWarehouseList) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipSpareInWarehouseEntity> list = new ArrayList<EquipSpareInWarehouseEntity>();
		for (EquipSpareInWarehouse equipSpareInWarehouse : inWarehouseList) {
			EquipSpareInWarehouseEntity innerData = equipSpareInWarehouse.getInnerData();
			if (innerData.getId() == 0) {
				throw new RuntimeException("无法获取入库单主键");
			}
			EquipSpareInWarehouseEntity equipSpareInWarehouseEntity = new EquipSpareInWarehouseEntity();
			equipSpareInWarehouseEntity.setId(innerData.getId());
			equipSpareInWarehouseEntity.setRowStatus(0);
			list.add(equipSpareInWarehouseEntity);
		}
		boolean updateBatchById = updateBatchById(list, list.size());
		if (!updateBatchById) {
			throw new RuntimeException("删除失败！");
		}
		return resultBean;
	}

	public ResultDev selectBySomeField(EquipSpareInWarehouse equipSpareInWarehouse) {
		ResultDev resultDev = new ResultDev();
		EquipSpareInWarehouseEntity innerData = equipSpareInWarehouse.getInnerData();
		if (equipSpareInWarehouse.getPageNum() == 0) {
			equipSpareInWarehouse.setPageNum(1);
		}
		if (equipSpareInWarehouse.getPageSize() == 0) {
			equipSpareInWarehouse.setPageSize(10);
		}
		QueryWrapper<EquipSpareInWarehouseEntity> queryWrapper = new QueryWrapper<EquipSpareInWarehouseEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.orderByDesc("creation_time");
		if (StringUtils.isNotEmpty(innerData.getInWarehouseNumber())) {
			queryWrapper.like("in_warehouse_number", innerData.getInWarehouseNumber());
		}
		if (equipSpareInWarehouse.getInWarehouseStartTime() != null
				&& equipSpareInWarehouse.getInWarehouseEndTime() != null) {
			queryWrapper.between("in_warehouse_time", equipSpareInWarehouse.getInWarehouseStartTime(),
					equipSpareInWarehouse.getInWarehouseEndTime());
		}
		if (StringUtils.isNotEmpty(innerData.getWarehouseName())) {
			queryWrapper.like("warehouse_name", innerData.getWarehouseName());
		}
		if (StringUtils.isNotEmpty(innerData.getInWarehouseType())) {
			queryWrapper.like("in_warehouse_type", innerData.getInWarehouseType());
		}
		if (StringUtils.isNotEmpty(innerData.getUserDepartment())) {
			queryWrapper.like("user_department", innerData.getUserDepartment());
		}
		if (StringUtils.isNotEmpty(innerData.getSupplierName())) {
			queryWrapper.like("supplier_name", innerData.getSupplierName());
		}
		if (StringUtils.isNotEmpty(innerData.getAgent())) {
			queryWrapper.like("agent", innerData.getAgent());
		}
		if (StringUtils.isNotEmpty(innerData.getRequestNumber())) {
			queryWrapper.like("request_number", innerData.getRequestNumber());
		}
		Page<EquipSpareInWarehouseEntity> page = new Page<EquipSpareInWarehouseEntity>(
				equipSpareInWarehouse.getPageNum(), equipSpareInWarehouse.getPageSize(), true);
		IPage<EquipSpareInWarehouseEntity> selectPage = equipSpareInWarehouseDao.selectPage(page, queryWrapper);
		resultDev.setEntityList(selectPage.getRecords());
		resultDev.setPageCount((int) selectPage.getPages());
		resultDev.setTotalQuantity((int) selectPage.getTotal());
		return resultDev;
	}

	public ResultDev selectAll() {
		ResultDev resultDev = new ResultDev();
		QueryWrapper<EquipSpareInWarehouseEntity> queryWrapper = new QueryWrapper<EquipSpareInWarehouseEntity>();
		queryWrapper.eq("row_status", 1);
		List<EquipSpareInWarehouseEntity> selectList = equipSpareInWarehouseDao.selectList(queryWrapper);
		resultDev.setEntityList(selectList);
		return resultDev;
	}

}
